package global

import (
	"hcy-api/structs/idp"
	"hcy-api/structs/tables"
	"net"
	"sync"
)

// 本地全局缓存，每个都要写注释。之前遇到一个项目，特别喜欢用缓存，还不写注释

// AbnormalDevice 状态可疑的设备
type AbnormalDevice struct {
	HandsUpMsg interface{}
	Conn       net.Conn
}

// IdpCache idp 客户端缓存信息
type IdpCache struct {
	CpuPer         float64
	MemoryPer      float64
	LastCpuWarn    idp.CpuMsg
	LastMemoryWarn idp.MemoryMsg
	LastWinSession idp.WinSessionChangeMsg
	CPU            tables.IdpLogCpu
	Memory         tables.IdpLogMemory
	DiskPhysics    tables.IdpLogDisk
	DiskLogic      idp.RLogicDisk
	BoardBase      tables.IdpLogBoard
	BoardMain      idp.RMainBoard
	BoardBios      idp.RBios
	SysAbstract    tables.IdpLogSysAbstract
	HandsUpMsg     idp.HandsUpMsgForVi
}

// InfoCatchMaoForIdp idp 客户端缓存信息，key : *IdpCache .收到的消息都要先和这里比对，然后在走后面的逻辑,
var InfoCatchMaoForIdp sync.Map

// AbnormalDeviceForIdp idp中的状态可疑客户端
var AbnormalDeviceForIdp sync.Map

// TaskCatch 任务缓存 map[taskId]tables.IdpTask
var TaskCatch sync.Map

// clientInstructCatch 客户端指令列表缓存 map[clientId]map[cmd+argus]idp.PluginInstruct
var clientInstructCatch = make(map[int64]map[string]idp.PluginInstruct)

var Mu sync.Mutex

func GetClientInstructCatch(clientId int64) map[string]idp.PluginInstruct {
	Mu.Lock()
	defer Mu.Unlock()
	pm := clientInstructCatch[clientId]
	if pm == nil {
		pm = make(map[string]idp.PluginInstruct)
	}
	return pm
}

func SetClientInstructCatch(clientId int64, pm map[string]idp.PluginInstruct) {
	Mu.Lock()
	defer Mu.Unlock()
	clientInstructCatch[clientId] = pm
}

func DeleteClientInstructCatch(clientId int64) {
	Mu.Lock()
	defer Mu.Unlock()
	delete(clientInstructCatch, clientId)
}

// InstructResultMap 缓存实时指令的结果，因为tcp服务中的消息收发是异步执行的，为了解决前端页面同步实时显示消息的问题，添加这个缓存。
var InstructResultMap sync.Map
